﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Collections;

namespace SWEN3Application.Classes
{
    public class HttpClientMethod
    {

        private static HttpClient SetupHttpClient()
        {
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("http://localhost:57001/");
            // Add an Accept header for JSON format. 
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            return client;
        }

        public static ArrayList getAllRooms()
        {
            HttpClient client = SetupHttpClient();

            HttpResponseMessage response = client.GetAsync("api/Rooms").Result;

            if (response.IsSuccessStatusCode)
            {
                var rooms = response.Content.ReadAsAsync<IEnumerable<Rooms>>().Result;
                ArrayList roomList = new ArrayList();
                foreach (var r in rooms)
                {
                    roomList.Add(r);
                }
                return roomList;
            }
            else
            {
                return null;
            }

        }

        public static int CheckIn(BookingList b, Rooms r)
        {
            int BookingNo = 0;
            HttpClient client = SetupHttpClient();
            HttpResponseMessage response = client.PostAsJsonAsync("api/Booking", b).Result;

            if (response.IsSuccessStatusCode)
            {
                BookingNo = response.Content.ReadAsAsync<int>().Result;
                response = client.PostAsJsonAsync("api/Room/", r).Result;

                if (response.IsSuccessStatusCode)
                {
                    if (BookingNo != response.Content.ReadAsAsync<int>().Result)
                    {
                        BookingNo = -1;
                    }
                }
            }
            return BookingNo;
        }
        /*
        public Boolean checkAvaibility(String Username)
        {
            HttpClient client = SetupHttpClient();
            HttpResponseMessage response = client.GetAsync("api/UserAccount?Username=" + Username).Result;
            UserAccount user = response.Content.ReadAsAsync<UserAccount>().Result;
            if (user == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public UserAccount verifyUser(string Username, string Password)
        {
            HttpClient client = SetupHttpClient();
            HttpResponseMessage response = client.GetAsync("api/UserAccount?Username=" + Username + "&Password=" + Password).Result;
            UserAccount user = response.Content.ReadAsAsync<UserAccount>().Result;
            return user;
        }

        public Administrator getAdminById(int id)
        {
            HttpClient client = SetupHttpClient();
            HttpResponseMessage response = client.GetAsync("api/Administrator/" + id).Result;
            Administrator admin = response.Content.ReadAsAsync<Administrator>().Result;
            return admin;
        }

        public Customer getCustomerById(int id)
        {
            HttpClient client = SetupHttpClient();
            HttpResponseMessage response = client.GetAsync("api/Customer/" + id).Result;
            Customer customer = response.Content.ReadAsAsync<Customer>().Result;
            return customer;
        }

        public Boolean setActivation(Administrator AdminActivation)
        {
            HttpClient client = SetupHttpClient();
            HttpResponseMessage response = client.PutAsJsonAsync("api/Administrator/", AdminActivation).Result;
            Boolean sucessful = response.Content.ReadAsAsync<Boolean>().Result;
            return sucessful;
        }

        public Boolean CreateCompanyApi(Company c)
        {
            HttpClient client = SetupHttpClient();
            HttpResponseMessage response = client.PostAsJsonAsync("api/Company", c).Result;
            if (response.IsSuccessStatusCode)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Company getCompanyById(int id)
        {
            HttpClient client = SetupHttpClient();
            HttpResponseMessage response = client.GetAsync("api/Company/" + id).Result;
            Company company = response.Content.ReadAsAsync<Company>().Result;
            return company;
        }

        public Boolean updateAdmin(Administrator admin)
        {
            HttpClient client = SetupHttpClient();
            HttpResponseMessage response = client.PutAsJsonAsync("api/Administrator/" + admin.AdminId, admin).Result;
            if (response.IsSuccessStatusCode)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public ArrayList getAllCompany()
        {
            HttpClient client = SetupHttpClient();

            HttpResponseMessage response = client.GetAsync("api/Company").Result;

            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking! 
                var company = response.Content.ReadAsAsync<IEnumerable<Company>>().Result;
                ArrayList companyList = new ArrayList();
                foreach (var c in company)
                {
                    companyList.Add(c);
                }

                return companyList;
            }
            else
            {
                return null;
            }

        }

        public Boolean updateCompany(Company company)
        {
            HttpClient client = SetupHttpClient();
            HttpResponseMessage response = client.PutAsJsonAsync("api/Company/" + company.CompanyId, company).Result;
            if (response.IsSuccessStatusCode)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Boolean DeleteCompany(int id)
        {
            HttpClient client = SetupHttpClient();

            HttpResponseMessage response = client.DeleteAsync("api/Company/" + id).Result;
            if (response.IsSuccessStatusCode)
            {
                return true;
            }
            else
            {
                return false;
            }
        }*/
    }
}